package me.jessyan.mvparms.demo.util;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;


import com.platform.ai.control.R;

import me.jessyan.mvparms.demo.mvp.ui.adapter.BaseFragmentPagerAdapter;

/**
 * Created by 52248 on 2018/9/5.
 */

public class AnimationUtil {
    public static void onScaleAnimationBySpringWayOne(View view){
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(view,"scaleX",0.8f,1f);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(view,"scaleY",0.8f,1f);
        AnimatorSet set =new AnimatorSet();
        set.setDuration(500);
//        set.setInterpolator(new SpringScaleInterpolator(0.4f));
        set.playTogether(animatorX,animatorY);
        set.start();
    }

    public static void animRotationUp(View v) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation", 0f, 180f);
        anim.setDuration(100);// 动画持续时间
        anim.start();
    }

    public static void animRotationDown(View v) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation", 180f, 0f);
        anim.setDuration(100);// 动画持续时间
        anim.start();
    }

    public static void setTabLayoutWithArrow(TabLayout mTab, ViewPager mVp, String[] mTitles, boolean[] inverts, BaseFragmentPagerAdapter mPagerAdapter){
        for (int i = 0; i < mTitles.length; i++) {
            TabLayout.Tab tab = mTab.getTabAt(i);//获得每一个tab
            tab.setCustomView(R.layout.view_tab_title);//给每一个tab设置view
            TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tv_tab);
            textView.setText(mTitles[i]);//设置tab上的文字
            int position = i;
            if(i == 0){
                textView.setTextColor(Color.parseColor("#e4536b"));
                textView.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
            }
            tab.getCustomView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (inverts[position]) {
                        inverts[position] = false;
//                        AnimationUtil.animRotationUp(mTab.getTabAt(position).getCustomView().findViewById(R.id.iv_arrow));
//                        mTab.getTabAt(position).getCustomView().findViewById(R.id.iv_arrow).setRotation(180f);
                    } else {
                        inverts[position] = true;
//                        AnimationUtil.animRotationDown(mTab.getTabAt(position).getCustomView().findViewById(R.id.iv_arrow));
//                        mTab.getTabAt(position).getCustomView().findViewById(R.id.iv_arrow).setRotation(0f);
                    }
//                    ((RepertoryFragment)mPagerAdapter.getFragmentByPosition(position)).setInvert(inverts[position]? 1:0);
                    mVp.setCurrentItem(position, false);
                }
            });

        }

        mTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab == mTab.getTabAt(0)) {
                    mVp.setCurrentItem(0,false);
//                    ((RepertoryFragment)mPagerAdapter.getFragmentByPosition(0)).setInvert(inverts[0]? 1:0);
                }else if(tab == mTab.getTabAt(1)){
                    mVp.setCurrentItem(1,false);
//                    ((RepertoryFragment)mPagerAdapter.getFragmentByPosition(1)).setInvert(inverts[1]? 1:0);
                }else if(tab == mTab.getTabAt(2)){
                    mVp.setCurrentItem(2,false);
//                    ((RepertoryFragment)mPagerAdapter.getFragmentByPosition(2)).setInvert(inverts[2]? 1:0);
                }else if(tab == mTab.getTabAt(3)){
                    mVp.setCurrentItem(3,false);
                }
                if(tab == mTab.getTabAt(0) || tab == mTab.getTabAt(1) || tab == mTab.getTabAt(2) || tab == mTab.getTabAt(3)) {
                    if(tab == null){
                        return;
                    }
                    AnimationUtil.onScaleAnimationBySpringWayOne(tab.getCustomView().findViewById(R.id.tv_tab));
                    ((TextView) tab.getCustomView().findViewById(R.id.tv_tab)).setTextColor(Color.parseColor("#e4536b"));
                    ((TextView) tab.getCustomView().findViewById(R.id.tv_tab)).setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab == mTab.getTabAt(0)) {
                    mVp.setCurrentItem(0,false);
                }else if(tab == mTab.getTabAt(1)){
                    mVp.setCurrentItem(1,false);
                }else if(tab == mTab.getTabAt(2)){
                    mVp.setCurrentItem(2,false);
                }else if(tab == mTab.getTabAt(3)){
                    mVp.setCurrentItem(3,false);
                }
                if(tab == mTab.getTabAt(0) || tab == mTab.getTabAt(1) || tab == mTab.getTabAt(2)|| tab == mTab.getTabAt(3)) {
                    if(tab == null){
                        return;
                    }
                    ((TextView) tab.getCustomView().findViewById(R.id.tv_tab)).setTextColor(Color.parseColor("#4a4a4a"));
                    ((TextView) tab.getCustomView().findViewById(R.id.tv_tab)).setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) { 

            }
        });
    }




}
